import { Controller } from "../../mvc/core/Controller";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SCrossMassacreChoose } from "../../../Message/Public/SCrossMassacreChoose";
import { LevelLimitDefin } from "../../common/defin/LevelLimitDefin";
import { CrossMassacreHintBtn } from "../view/crossMassacre/CrossMassacreHintBtn";
import { SCrossMassacreTaskinfo } from "../../../Message/Game/SCrossMassacreTaskinfo";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { Alert } from "../../../com/mui/controls/Alert";
import { GameProxy } from "../mvc/GameProxy";
import { Language } from "../../../extend/language/Language";
import { SCrossMassacreStart } from "../../../Message/Public/SCrossMassacreStart";
import { DisplayUtil } from "../../common/DisplayUtil";
import { SCrossMassacreChooseList } from "../../../Message/Public/SCrossMassacreChooseList";
import { CrossMassacreEnterWin } from "../view/crossMassacre/CrossMassacreEnterWin";
import { ECountryPrivilege } from "../../../Message/Game/ECountryPrivilege";
import { MsgManager } from "../manager/MsgManager";
import { CrossMassacreServerData } from "../view/crossMassacre/data/CrossMassacreServerData";
import { EntityUtil } from "../scene/layer/utils/EntityUtil";
import { ClockManager } from "../manager/ClockManager";
import { SCrossCountryInfo } from "../../../Message/Game/SCrossCountryInfo";
import { EMapPointType } from "../../../Message/Public/EMapPointType";
import { _EMapPointTypeCrossCountry1 } from "../../../EMapPointType/_EMapPointTypeCrossCountry1";
type int = number;
//class CrossMassacreController
    
    export  class CrossMassacreController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossMassacreChoose, this.readyHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossMassacreStart, this.startHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossMassacreEnd, this.endHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateCrossMassacreTaskinfo, this.crossMassacreTaskinfoHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossMassacreList, this.listUpdateHandler);
            Dispatcher.addEventListener(EventName.CrossMassacreShowModule, this.showModuleHandler);
            Dispatcher.addEventListener(EventName.CrossPVP_getServerListSuccess, this.serverListHandler);
            Dispatcher.addEventListener(EventName.CrossMassacreSelectServerReq, this.selectServerHandler);
            Dispatcher.addEventListener(EventName.CrossMassacreEnterReq, this.enterReqHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.sceneUpdateHandler);
            Dispatcher.addEventListener(EventName.CountryMoneyUpdate, this.countryMoneyUpdateHandler);
            return;
        }

        /* internal  */sceneUpdateHandler(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */readyHandler(arg1: MessageBlock): void
        {
            cache.crossMassacre.readyData = arg1.messageBase as SCrossMassacreChoose;
            if (cache.role.roleInfo.level < LevelLimitDefin.CrossMassacreShowIconLevel) 
            {
                return;
            }
            this.getCrossMassacreTask();
            if (this._icon == null) 
            {
                this._icon = new CrossMassacreHintBtn();
            }
            this._icon.show();
            this._icon.updateSelectCountDown(cache.crossMassacre.readyData);
            return;
        }

        /* internal  */crossMassacreTaskinfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SCrossMassacreTaskinfo;
            cache.crossMassacre.canGetTask = loc1.canGet;
            this.getCrossMassacreTask();
            return;
        }

        /* internal  */getCrossMassacreTask(arg1: DataEvent=null): void
        {
            var e: DataEvent=null;
            var onClickHandler: Function;

            var loc1;
            e = arg1;
            if ((GameMapUtil.curMapState.isCrossCountry || GameMapUtil.curMapState.isTaijiCity) && cache.crossMassacre.canGetTask && cache.role.roleInfo.level >= LevelLimitDefin.CrossMassacreShowIconLevel && this._alert == null) 
            {
                onClickHandler = (arg1: int): void
                {
                    _alert = null;
                    if (arg1 == Alert.OK) 
                    {
                        GameProxy.taskProxy.getTaskReq(0, cache.crossMassacre.taskCode);
                    }
                    return;
                }
                this._alert = Alert.show(Language.getString(28550), null, Alert.OK | Alert.CANCEL, null, onClickHandler);
            }
            return;
        }

        /* internal  */startHandler(arg1: MessageBlock): void
        {
            cache.crossMassacre.isInCrossMassacre = true;
            Dispatcher.dispatchEvent(new DataEvent(EventName.CountryBuffSkillOpened));
            if (cache.role.roleInfo.level < LevelLimitDefin.CrossMassacreShowIconLevel) 
            {
                return;
            }
            this.getCrossMassacreTask();
            cache.crossMassacre.startData = arg1.messageBase as SCrossMassacreStart;
            if (this._icon == null) 
            {
                this._icon = new CrossMassacreHintBtn();
            }
            this._icon.show();
            this._icon.updateEndCountDown(cache.crossMassacre.startData);
            Dispatcher.addEventListener(EventName.ChangeScene, this.getCrossMassacreTask);
            return;
        }

        /* internal  */endHandler(arg1: MessageBlock): void
        {
            cache.crossMassacre.isInCrossMassacre = false;
            DisplayUtil.removeMe(this._alert);
            Dispatcher.dispatchEvent(new DataEvent(EventName.CountryBuffSkillClose));
            Dispatcher.removeEventListener(EventName.ChangeScene, this.getCrossMassacreTask);
            if (this._icon == null) 
            {
                return;
            }
            if (!this._icon.isHide) 
            {
                this._icon.hide();
            }
            this._icon.dispose();
            this._icon = null;
            this._alert = null;
            return;
        }

        /* internal  */listUpdateHandler(arg1: MessageBlock): void
        {
            cache.crossMassacre.chooseList = arg1.messageBase as SCrossMassacreChooseList;
            if (this._win == null) 
            {
                return;
            }
            this._win.updateServerList(cache.crossMassacre.serverListData);
            this._win.updateMassacreData(cache.crossMassacre.getMassacreSeverData());
            this._win.updateDefenceData(cache.crossMassacre.getDefenceServerData());
            return;
        }

        /* internal  */showModuleHandler(arg1: DataEvent): void
        {
            if (this._win == null) 
            {
                this._win = new CrossMassacreEnterWin();
                this._win.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.addToStageHandler);
            }
            if (this._win.isHide) 
            {
                this._win.show();
            }
            else 
            {
                this._win.hide();
            }
            return;
        }

        /* internal  */addToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.CountryGetMoneyRecord));
            GameProxy.country.getCrossCountryList();
            this.updateModuleAll();
            return;
        }

        /* internal  */updateModuleAll(): void
        {
            this._win.updateEnterCountDown(cache.crossMassacre.readyData);
            this._win.updateMassacreData(cache.crossMassacre.getMassacreSeverData());
            this._win.updateDefenceData(cache.crossMassacre.getDefenceServerData());
            return;
        }

        /* internal  */serverListHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            if (loc1 == null) 
            {
                return;
            }
            cache.crossMassacre.serverListWithoutChooseNum = loc1;
            if (this._win == null) 
            {
                return;
            }
            this._win.updateServerList(cache.crossMassacre.serverListData);
            return;
        }

        /* internal  */selectServerHandler(arg1: DataEvent): void
        {
            if (!cache.country.doIHavePrivilege(ECountryPrivilege._ECountryPrivilegeStartKill)) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80501));
                return;
            }
            var loc1=arg1.data as CrossMassacreServerData;
            if (loc1 == null || loc1.info == null || loc1.info.serverKey == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80506));
                return;
            }
            if (EntityUtil.isSameServerByProxyAndServer(loc1.info.serverKey.proxyId, loc1.info.serverKey.serverId)) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80507));
                return;
            }
            cache.crossMassacre.curSelectMassacreServer = loc1;
            var loc2=Language.getStringByParam(80502, EntityUtil.getProxyNameByProxyAndServer(loc1.info.serverKey.proxyId, loc1.info.serverKey.serverId));
            Alert.show(loc2, null, Alert.OK | Alert.CANCEL, null, this.alertSelectCloseHandler);
            return;
        }

        /* internal  */alertSelectCloseHandler(arg1: int): void
        {
            if (arg1 != Alert.OK) 
            {
                return;
            }
            var loc1=cache.crossMassacre.curSelectMassacreServer;
            if (loc1 == null || loc1.info == null || loc1.info.serverKey == null) 
            {
                return;
            }
            GameProxy.country.selectCrossMassacreServer(loc1.info.serverKey.proxyId, loc1.info.serverKey.serverId);
            return;
        }

        /* internal  */enterReqHandler(arg1: DataEvent): void
        {
            var loc1=cache.crossMassacre.startData;
            if (loc1 == null || ClockManager.instance.nowDate.time < loc1.openDt.time) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80503));
                return;
            }
            if (ClockManager.instance.nowDate.time > loc1.endDt.time) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80504));
                return;
            }
            var loc2=cache.crossMassacre.getMassacreSeverData();
            if (loc2 == null || loc2.info == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80505));
                return;
            }
            var loc3;
            (loc3 = new SCrossCountryInfo()).money = loc2.countryMoney;
            loc3.serverKey = loc2.info.serverKey;
            Dispatcher.dispatchEvent(new DataEvent(EventName.CrossPVP_enterCrossCountry, {"countryKey": loc3, "pointType": _EMapPointTypeCrossCountry1}));
            return;
        }

        /* internal  */countryMoneyUpdateHandler(arg1: DataEvent): void
        {
            if (this._win == null) 
            {
                return;
            }
            this._win.updateServerList(cache.crossMassacre.serverListData);
            this._win.updateDefenceData(cache.crossMassacre.getDefenceServerData());
            return;
        }

        private /* var */_icon: CrossMassacreHintBtn;

        private /* var */_win: CrossMassacreEnterWin;

        private /* var */_alert: Object /* flash.display.Sprite */;
    }
